package com.huaji.eat;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class AddFoodActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_food);

        Button btn_add = findViewById(R.id.btn_add);
        Button btn_back = findViewById(R.id.btn_back_add);
        EditText edit_name = findViewById(R.id.input_food_name);

        btn_add.setOnClickListener(v -> {
            Data.foods.add(edit_name.getText().toString());
            Toast.makeText(AddFoodActivity.this, "成功", Toast.LENGTH_LONG).show();
        });

        btn_back.setOnClickListener(v -> finish());
    }

    @Override
    protected void onPause() {
        super.onPause();

        try {
            FileOutputStream stream = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Data.bin");
            ObjectOutputStream object = new ObjectOutputStream(stream);
            object.writeObject(Data.foods);
            stream.close();
            object.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}